<template>
  <div class="data-pool-panel">
    <div class="panel-header">
      <h3>数据池</h3>
      <button class="add-btn" @click="addDataItem">+ 添加数据</button>
    </div>
    
    <div class="panel-content">
      <!-- 数据项列表 -->
      <div v-if="dataItems.length > 0" class="data-items-list">
        <div 
          v-for="item in dataItems" 
          :key="item.id"
          class="data-item"
          :class="{ 'selected': selectedDataItemId === item.id }"
          @click="selectDataItem(item.id)"
        >
          <div class="item-header">
            <div class="item-name">{{ item.name }}</div>
            <div class="item-type">{{ item.type }}</div>
          </div>
          <div class="item-value">{{ formatValue(item.value) }}</div>
          <div class="item-actions">
            <button class="action-btn edit-btn" @click.stop="editDataItem(item)">✏️</button>
            <button class="action-btn delete-btn" @click.stop="deleteDataItem(item.id)">🗑️</button>
          </div>
        </div>
      </div>
      
      <!-- 空状态提示 -->
      <div v-else class="empty-tip">
        <p>数据池为空，请添加数据项</p>
      </div>
      
      <!-- 选中数据项的详情 -->
      <div v-if="selectedDataItem" class="data-item-detail">
        <h4>数据项详情</h4>
        
        <div class="detail-item">
          <div class="detail-label">名称</div>
          <div class="detail-value">{{ selectedDataItem.name }}</div>
        </div>
        
        <div class="detail-item">
          <div class="detail-label">类型</div>
          <div class="detail-value">{{ selectedDataItem.type }}</div>
        </div>
        
        <div class="detail-item">
          <div class="detail-label">值</div>
          <div class="detail-value">
            <pre>{{ JSON.stringify(selectedDataItem.value, null, 2) }}</pre>
          </div>
        </div>
        
        <div class="detail-item" v-if="selectedDataItem.description">
          <div class="detail-label">描述</div>
          <div class="detail-value">{{ selectedDataItem.description }}</div>
        </div>
      </div>
    </div>
    
    <!-- 添加/编辑数据项对话框 -->
    <div class="data-item-modal" v-if="showAddDataItemModal || showEditDataItemModal">
      <div class="modal-content">
        <div class="modal-header">
          <h4>{{ showEditDataItemModal ? '编辑数据项' : '添加数据项' }}</h4>
          <button class="close-btn" @click="closeDataItemModal">×</button>
        </div>
        
        <div class="modal-body">
          <div class="form-item">
            <label>名称</label>
            <input type="text" v-model="editingDataItem.name" placeholder="请输入数据项名称" />
          </div>
          
          <div class="form-item">
            <label>类型</label>
            <select v-model="editingDataItem.type">
              <option value="string">字符串</option>
              <option value="number">数字</option>
              <option value="boolean">布尔值</option>
              <option value="array">数组</option>
              <option value="object">对象</option>
            </select>
          </div>
          
          <div class="form-item">
            <label>值</label>
            <textarea 
              v-model="editingDataItemValueStr" 
              placeholder="请输入JSON格式的值"
              rows="5"
            ></textarea>
            <div class="error-message" v-if="valueError">{{ valueError }}</div>
          </div>
          
          <div class="form-item">
            <label>描述</label>
            <textarea 
              v-model="editingDataItem.description" 
              placeholder="请输入数据项描述（可选）"
              rows="3"
            ></textarea>
          </div>
        </div>
        
        <div class="modal-footer">
          <button class="btn" @click="closeDataItemModal">取消</button>
          <button class="btn primary" @click="saveDataItem">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { v4 as uuidv4 } from 'uuid'
import { useDataPoolStore, type DataItem } from '@/stores/dataPool'

const dataPoolStore = useDataPoolStore()

// 获取所有数据项
const dataItems = computed(() => dataPoolStore.getAllDataItems)

// 选中的数据项ID
const selectedDataItemId = ref<string | null>(null)

// 选中的数据项
const selectedDataItem = computed(() => {
  if (!selectedDataItemId.value) return null
  return dataPoolStore.getDataItemById(selectedDataItemId.value)
})

// 选择数据项
const selectDataItem = (id: string) => {
  selectedDataItemId.value = id
}

// 格式化数据项值显示
const formatValue = (value: any): string => {
  if (value === null || value === undefined) return '空'
  if (typeof value === 'object') return '[对象]'
  return String(value)
}

// 添加/编辑数据项相关状态
const showAddDataItemModal = ref(false)
const showEditDataItemModal = ref(false)
const editingDataItem = ref<DataItem>({
  id: '',
  name: '',
  type: 'string',
  value: '',
  description: ''
})
const editingDataItemValueStr = ref('')
const valueError = ref('')

// 打开添加数据项对话框
const addDataItem = () => {
  editingDataItem.value = {
    id: uuidv4(),
    name: '',
    type: 'string',
    value: '',
    description: ''
  }
  editingDataItemValueStr.value = ''
  valueError.value = ''
  showAddDataItemModal.value = true
}

// 打开编辑数据项对话框
const editDataItem = (item: DataItem) => {
  editingDataItem.value = { ...item }
  editingDataItemValueStr.value = JSON.stringify(item.value, null, 2)
  valueError.value = ''
  showEditDataItemModal.value = true
}

// 删除数据项
const deleteDataItem = (id: string) => {
  if (confirm('确定要删除此数据项吗？')) {
    dataPoolStore.removeDataItem(id)
    if (selectedDataItemId.value === id) {
      selectedDataItemId.value = null
    }
  }
}

// 关闭数据项对话框
const closeDataItemModal = () => {
  showAddDataItemModal.value = false
  showEditDataItemModal.value = false
}

// 保存数据项
const saveDataItem = () => {
  // 验证名称
  if (!editingDataItem.value.name.trim()) {
    alert('请输入数据项名称')
    return
  }
  
  // 解析并验证值
  try {
    const parsedValue = JSON.parse(editingDataItemValueStr.value)
    editingDataItem.value.value = parsedValue
    valueError.value = ''
  } catch (error) {
    valueError.value = `JSON解析错误: ${(error as Error).message}`
    return
  }
  
  // 保存数据项
  dataPoolStore.addDataItem(editingDataItem.value)
  
  // 关闭对话框
  closeDataItemModal()
}
</script>

<style lang="scss" scoped>
.data-pool-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
  
  .panel-header {
    padding: 12px 16px;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    h3 {
      margin: 0;
      font-size: 16px;
      font-weight: 600;
      color: #333;
    }
    
    .add-btn {
      padding: 4px 8px;
      background-color: #409eff;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 12px;
      
      &:hover {
        background-color: #66b1ff;
      }
    }
  }
  
  .panel-content {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    
    .empty-tip {
      text-align: center;
      color: #999;
      padding: 40px 0;
    }
    
    .data-items-list {
      margin-bottom: 16px;
      
      .data-item {
        padding: 10px;
        background-color: #fff;
        border: 1px solid #e0e0e0;
        border-radius: 4px;
        margin-bottom: 8px;
        cursor: pointer;
        transition: all 0.2s;
        
        &:hover {
          background-color: #f5f7fa;
        }
        
        &.selected {
          border-color: #409eff;
          background-color: #ecf5ff;
        }
        
        .item-header {
          display: flex;
          justify-content: space-between;
          margin-bottom: 6px;
          
          .item-name {
            font-weight: 500;
            color: #333;
          }
          
          .item-type {
            color: #909399;
            font-size: 12px;
          }
        }
        
        .item-value {
          color: #606266;
          font-size: 13px;
          margin-bottom: 6px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        
        .item-actions {
          display: flex;
          justify-content: flex-end;
          gap: 4px;
          
          .action-btn {
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: transparent;
            border: none;
            cursor: pointer;
            font-size: 12px;
            
            &:hover {
              background-color: #f5f5f5;
              border-radius: 4px;
            }
            
            &.delete-btn:hover {
              color: #f56c6c;
            }
          }
        }
      }
    }
    
    .data-item-detail {
      padding: 12px;
      background-color: #f5f7fa;
      border: 1px solid #e0e0e0;
      border-radius: 4px;
      
      h4 {
        margin: 0 0 12px 0;
        font-size: 14px;
        color: #333;
      }
      
      .detail-item {
        margin-bottom: 10px;
        
        .detail-label {
          font-size: 12px;
          color: #909399;
          margin-bottom: 4px;
        }
        
        .detail-value {
          color: #606266;
          
          pre {
            margin: 0;
            padding: 8px;
            background-color: #f8f8f8;
            border-radius: 4px;
            overflow-x: auto;
            font-family: monospace;
            font-size: 12px;
          }
        }
      }
    }
  }
  
  .data-item-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    animation: fadeIn 0.2s ease-in-out;
    
    .modal-content {
      width: 500px;
      max-width: 90%;
      background-color: #fff;
      border-radius: 4px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
      
      .modal-header {
        padding: 12px 16px;
        border-bottom: 1px solid #e0e0e0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        h4 {
          margin: 0;
          font-size: 16px;
          color: #333;
        }
        
        .close-btn {
          background: none;
          border: none;
          font-size: 20px;
          cursor: pointer;
          color: #909399;
          
          &:hover {
            color: #333;
          }
        }
      }
      
      .modal-body {
        padding: 16px;
        
        .form-item {
          margin-bottom: 16px;
          
          label {
            display: block;
            margin-bottom: 6px;
            font-size: 14px;
            color: #606266;
          }
          
          input, select, textarea {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #dcdfe6;
            border-radius: 4px;
            font-size: 14px;
            color: #606266;
            background-color: #fff;
            transition: border-color 0.2s;
            box-sizing: border-box;
            
            &:focus {
              outline: none;
              border-color: #409eff;
            }
            
            &::placeholder {
              color: #c0c4cc;
            }
          }
          
          .error-message {
            color: #f56c6c;
            font-size: 12px;
            margin-top: 4px;
          }
        }
      }
      
      .modal-footer {
        padding: 16px;
        border-top: 1px solid #e0e0e0;
        display: flex;
        justify-content: flex-end;
        gap: 8px;
        
        .btn {
          padding: 8px 16px;
          border-radius: 4px;
          font-size: 14px;
          cursor: pointer;
          transition: all 0.2s;
          border: 1px solid #dcdfe6;
          background-color: #fff;
          color: #606266;
          
          &:hover {
            background-color: #f5f7fa;
          }
          
          &.primary {
            background-color: #409eff;
            border-color: #409eff;
            color: #fff;
            
            &:hover {
              background-color: #66b1ff;
              border-color: #66b1ff;
            }
          }
        }
      }

    }
  }
}</style>
